package com.engineering.elink.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.MessageSelectCo;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.dao.MessageMapper;
import com.engineering.elink.dao.UserMapper;
import com.engineering.elink.domain.Message;
import com.engineering.elink.service.MessageService;

@Service
public class MessageServiceImpl implements MessageService{

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private MessageMapper messageMapper;
	
	@Override
	public Map<String, Object> getMessageList(MessageSelectCo messageSelectCo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", messageMapper.selectMessageList((messageSelectCo)));
		map.put("Pagination", new Pagination(messageSelectCo, messageMapper.selectMessageListCount(messageSelectCo)));
		return map;
	}

	@Override
	public boolean sendMessage(String userId, String byUserId, String content) {
		if (!userId.equals(byUserId)) {
			 Map<String, Object> map = userMapper.selectByUserId(byUserId);
			 if (map != null) {
				 Message message = new Message();
					message.setUserId(userId);
					message.setByUserId(byUserId);
					message.setContent(content);
					message.setCreateTime(new Date());
					message.setUserDeleteStatus(0);
					message.setByUserDeleteStatus(0);
					messageMapper.insertSelective(message);
					return true;
			 }
		}
		return false;
	}

	@Override
	public boolean removeMessage(String userId, int messageId) {
		Message message = messageMapper.selectByPrimaryKey(messageId);
		if (message != null ) {
			System.out.println("message.toString()="+message.toString());
			if (userId.equals(message.getUserId()) && message.getUserDeleteStatus() == 0) {
				System.out.println(1);
				message.setUserDeleteStatus(1);
			} else if (userId.equals(message.getByUserId()) && message.getByUserDeleteStatus() == 0) {
				System.out.println(2);
				message.setByUserDeleteStatus(1);
			} else {
				System.out.println(3);
				return false;
			}
			System.out.println("messageToSql.toString()="+message.toString());
			messageMapper.updateByPrimaryKeySelective(message);
			return true;
		}
		return false;
	}

	@Override
	public boolean readMessage(String userId, int messageId) {
		Message message = messageMapper.selectByPrimaryKey(messageId);
		if (message != null ) {
			System.out.println("message.toString()="+message.toString());
			if (userId.equals(message.getUserId()) && message.getUserDeleteStatus() == 0 && message.getByUserReadStatus() == 0) {
				System.out.println(1);
				message.setUserReadStatus(1);
			} else if (userId.equals(message.getByUserId()) && message.getByUserDeleteStatus() == 0 && message.getUserReadStatus() == 0) {
				System.out.println(2);
				message.setByUserReadStatus(1);
			} else {
				return false;
			}
			System.out.println("messageToSql.toString()="+message.toString());
			messageMapper.updateByPrimaryKeySelective(message);
			return true;
		}
		return false;
	}
	@Override
	public boolean clearMessage(String userId) {
		messageMapper.clearMessage(userId);
		return true;
	}
}
